package com.suray.rgv.instance;

import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.utils.LoggerType;
import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.basic.wcs.utils.ModuleType;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.rgv.IRgvManager;
import com.suray.rgv.ISeerRgvMapManage;
import com.suray.rgv.RgvInterface;
import com.suray.type.RgvMapTagDirection;
import com.suray.wcs.res.enums.RgvType;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 构造车辆实体，传递地图信息处理
 */
public class RgvInstance {

    public static Map<String, RgvInterface> rgvs = new HashMap<>();

    public static ISeerRgvMapManage rgvMapManage;

    public static IRgvManager rgv60Creater;
    public static IRgvManager rgvRollerCreater;
    public static IRgvManager rgvSMCreater;
    public static IRgvManager rgvForkCreater;
    public static IRgvManager rgvSonCreater;

    public static Set<RgvInterface> getAllRgvs(RgvType rgvType) {
        Set<RgvInterface> rgvList = new HashSet<>();

        for (RgvInterface rgv : rgvs.values()) {
            if (rgv.getRgvType() == rgvType) {
                rgvList.add(rgv);
            }
        }
        return rgvList;
    }

    /**
     * 插入小车对象
     * @param rgvNo
     * @param serialNum
     * @param ip
     * @param port
     * @param rgvType
     */
    public static RgvInterface newRgv(int rgvNo, String serialNum, String ip, Integer port, ModuleType rgvType) {
        if (StringUtils.isEmpty(serialNum)) {
            LoggerUtil.info(ModuleType.RGV, LoggerType.WARN, null, null, "添加小车失败，序列号不能为空", RgvInstance.class);
            throw new RuntimeException("添加小车失败，序列号不能为空");
        }

        if (StringUtils.isEmpty(ip)) {
            LoggerUtil.info(ModuleType.RGV, LoggerType.WARN, null, serialNum, "添加小车失败，ip不能为空", RgvInstance.class);
            throw new RuntimeException("添加小车失败，ip不能为空");
        }

        LoggerUtil.info(ModuleType.RGV, LoggerType.WARN, null, serialNum, "添加小车, ip:" + ip, RgvInstance.class);

        RgvInterface rgv;
        switch (rgvType) {
            case RGV_60:
                rgv = rgv60Creater.newRgv(rgvNo, serialNum, ip, port);
                break;
            case RGV_ROLLER:
                rgv = rgvRollerCreater.newRgv(rgvNo, serialNum, ip, port);
                break;
            case RGV_SON_MOTHER:
                rgv = rgvSMCreater.newRgv(rgvNo, serialNum, ip, port);
                break;
            case RGV_FORK:
                rgv = rgvForkCreater.newRgv(rgvNo, serialNum, ip, port);
                break;
            case RGV_SON:
                rgv = rgvSonCreater.newRgv(rgvNo, serialNum, ip, port);
                break;
            default:
                throw new RuntimeException("不支持该类型的小车：" + rgvType);
        }
        rgvs.put(serialNum, rgv);
        return rgv;
    }

    /**
     * 获取小车对象
     * @param serialNum
     * @return
     */
    public static RgvInterface get(String serialNum) {
        if (serialNum == null) {
            LoggerUtil.info(ModuleType.RGV, LoggerType.WARN, null, null, "获取小车，序列号不能为空", RgvInstance.class);
            return null;
        }

        return rgvs.get(serialNum);
    }

    /**
     * 删除小车
     * @param serialNum
     */
    public static boolean deleteRgv(String serialNum) {
        if (serialNum == null) {
            LoggerUtil.info(ModuleType.RGV, LoggerType.WARN, null, null, "删除小车，编号不能为空", RgvInstance.class);
            return false;
        }

        LoggerUtil.info(ModuleType.RGV, LoggerType.INFO, null, serialNum, "删除小车", RgvInstance.class);
        RgvInterface rgv = rgvs.get(serialNum);
        if (rgv != null) {
            rgv.destroy();
        }
        return rgvs.remove(serialNum) != null;
    }

    /**
     * 发送地图给小车
     * @param mapNodes
     * @return
     */
    public static void generalMap(List<Node> mapNodes, ModuleType rgvType,
                                  String wareHouse,RgvMapTagDirection direction) throws IOException {
        String paperFilePath = WcsFuncUtil.getPaperFilePath(rgvType, wareHouse);
        if (rgvType == ModuleType.RGV_60) {
            rgv60Creater.generalMap(paperFilePath,mapNodes, direction);
        } else if (rgvType == ModuleType.RGV_ROLLER) {
            rgvRollerCreater.generalMap(paperFilePath,mapNodes, direction);
        }
    }

    /**
     * 加载地图位移量
     * @param inputStream 地图输入流
     * @param mapNodes 地图节点
     * @param wareHouse 库区
     * @param rgvType 小车类型
     * @param direction 节点方向
     * @throws IOException
     */
    public static void loadRgvSmap(Integer layer,InputStream inputStream, List<Node> mapNodes, String wareHouse, ModuleType rgvType,
                                   RgvMapTagDirection direction) throws IOException {
        String paperFilePath = WcsFuncUtil.getPaperFilePath(rgvType, wareHouse);
        if (rgvType == ModuleType.RGV_60) {
            rgv60Creater.loadRgvSmap(layer,paperFilePath,inputStream,mapNodes, direction);
        } else if (rgvType == ModuleType.RGV_ROLLER) {
            rgvRollerCreater.loadRgvSmap(layer,paperFilePath,inputStream,mapNodes, direction);
        }
    }


    public static void loadMapLayer(String warehouseName,ModuleType rgvType) {
        String paperFilePath = WcsFuncUtil.getPaperFilePath(rgvType, warehouseName);
        String mapPath = paperFilePath + File.separator + "wcs_map";
        if (rgvType == ModuleType.RGV_60) {
            rgv60Creater.loadMapLayer(mapPath);
        } else if (rgvType == ModuleType.RGV_ROLLER) {
            rgvRollerCreater.loadMapLayer(mapPath);
        } else if (rgvType == ModuleType.RGV_SON_MOTHER) {
            rgvSMCreater.loadMapLayer(mapPath);
        }
    }
}
